<?php
require_once(APP.'libs'.DS.'system_properties.php');
class LoginsController extends Controller{
	var $layout='';
	var $uses=array('Login');
	
	// 后台用户登录
	function login($updatePassword=0){
		session_start();
		if(!empty($_SESSION['admin_user_id'])){
			$this->redirect(SystemProperties::$app_host."/user/index");
			return;
		}
		$userName=$this->data['login']['name'];
		$userPassword=$this->data['login']['password'];
		if(empty($userName) || empty($userPassword)){
			if($updatePassword==0){
			$this->set("validate","数据填写不完成");
			}
			$this->render(null,'login_layout',"login");
			return;
		}
		$password=md5($userPassword);
		$data=$this->Login->islogin($userName,$password);
		if(empty($data)){
			$this->set("validate","用户名或者密码不正确");
			$this->render(null,'login_layout',"login");
			return;
		}
		if (($data['0']['g_logins']['isguest']) == 0){
			$_SESSION['admin_user_id'] = $userName;
			$_SESSION['guest_user_id'] = '';
			$this->redirect(SystemProperties::$app_host."/user/index");
		}else if (($data['0']['g_logins']['isguest']) == 1){
			$_SESSION['admin_user_id'] = '';
			$_SESSION['guest_user_id'] = $userName;
			$_SESSION['guest_user_pids'] = explode(',',$data['0']['g_logins']['guest_pids']);
			$this->redirect(SystemProperties::$app_host."/product/showMyProductList");
		}
		return;
	}
	 // 后台用户退出
	function logout(){
		session_start();
		$_SESSION['admin_user_id']=null;
		$this->redirect(SystemProperties::$app_host."/Logins/login/1");
		return;
	}
	//修改密码页面
	function updatePassword(){
		$userName=$this->data['login']['name'];
		$userPassword=$this->data['login']['password'];
		$userNewPassword=$this->data['login']['newPassword'];
		if(empty($userName) || empty($userPassword) || empty($userNewPassword)){
			if(!empty($this->data['login']['status'])){
				$this->set("validate","数据填写不完成");
			}
			$this->render(null,'login_layout',"updatePassword");
			return;
		}
		$password=md5($userPassword);
		$data=$this->Login->islogin($userName,$password);
		if(empty($data)){
			$this->set("validate","用户名密码不正确");
			$this->render(null,'login_layout',"updatePassword");
			return;
		}
		$newpassword=md5($userNewPassword);
		$this->Login->updatePassword($userName,$password,$newpassword);
		$this->redirect(SystemProperties::$app_host."/Logins/login/1");
		return;
		
	}
	
}
?>